package com.jjw.springbootcvs.interceptor;

import com.jjw.springbootcvs.config.BootCvsProperties;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.sound.sampled.Port;

/**
 * @author xiaojuan
 * @Date 2024/11/4
 * @Description
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class LoginInterceptor implements HandlerInterceptor {

    private final BootCvsProperties bootCvsProperties;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
      /*  //判断当前拦截到的是Controller的方法还是其他资源
        if (!(handler instanceof HandlerMethod)) {
            //当前拦截到的不是动态方法，直接放行
            return true;
        }
*/
        HttpSession session = request.getSession();
        Object user = session.getAttribute(bootCvsProperties.getSessionKey());
        if (null==user){
            log.warn("当前用户未登录，请求路径:{}", request.getRequestURI());
            request.getRequestDispatcher("/login").forward(request,response);
            return false;
        }
        return true;
    }



}
